#include "rotationbutton.h"
#include <QPainter>
#include <QPainterPath>

rotationButton::rotationButton(QWidget *parent) : QAbstractButton(parent)
{
    id = 0;
    setCheckable(true);
    setFixedSize(10, 10);
}

int rotationButton::getId()const
{
    return id;
}

void rotationButton::setId(int id)
{
    this->id = id;
}

void rotationButton::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    QRectF drawRect= this->rect();
    QPainterPath drawPath;
    drawPath.addRoundedRect(drawRect, 100, 100, Qt::RelativeSize);
    //按路径裁剪
    painter.setClipPath(drawPath);

    if(isChecked())
    {
        painter.fillRect(drawRect, QColor(236, 65, 65));
    }
    else
    {
        painter.fillRect(drawRect, QColor(128, 128, 128, 128));
    }
}

void rotationButton::enterEvent(QEvent *event)
{
    if(!isChecked())
    {
        setChecked(true);
    }
    emit entered(id);

    return QAbstractButton::enterEvent(event);
}

void rotationButton::leaveEvent(QEvent *event)
{
    return QAbstractButton::leaveEvent(event);
}
